package com.wanmait.deliverymangementsystem.managecontroller;

import com.github.pagehelper.PageInfo;
import com.wanmait.deliverymangementsystem.pojo.Nickname;
import com.wanmait.deliverymangementsystem.pojo.Orders;
import com.wanmait.deliverymangementsystem.pojo.Runner;
import com.wanmait.deliverymangementsystem.service.NicknameService;
import com.wanmait.deliverymangementsystem.service.OrdersService;
import com.wanmait.deliverymangementsystem.service.RunnerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manage")
public class ManageRunnerController {
    @Resource
    private RunnerService runnerService;
    @Resource
    private NicknameService nicknameService;
    @Resource
    private OrdersService ordersService;
    @GetMapping("runnerList")
    @ModelAttribute("runnerList")
    public List<Runner> runnerList(Model model){
        List<Runner> runnerList =runnerService.runnerList();
        List<Nickname> nicknameList=nicknameService.findBeforeAdd();
        model.addAttribute("nicknameList",nicknameList);
        return runnerList;
    }

    @GetMapping("banRunner")
    public String banRunner(Integer id, Boolean status){
        runnerService.updateLoginStauts(id,status);
        return "redirect:/manage/runnerList";
    }

    @GetMapping("giveNickName")
    public String giveNickName(Integer runner,Integer id){
        System.out.println(runner+"((((((_____"+id+".....");
        runnerService.giveNickName(runner,id);
        return "redirect:/manage/runnerList";
    }

    @GetMapping("runnerPoint")
    @ModelAttribute("runnerPageInfo")
    public PageInfo<Runner> runnerPoint(Integer id, Integer page,Model model){
        Orders orders =ordersService.findOrder(id);
        Integer city =  orders.getTask().getUseraddress().getCity().getId();
     PageInfo<Runner> runnerPageInfo =runnerService.findByCityID(page,city);
     model.addAttribute("orderId",id);
        return runnerPageInfo;
    }

    @GetMapping("searchRunners")
    @ResponseBody
    public Map<String, Object> runnerPageInfo(String name,String phone,Model model){
        List<Nickname> nicknameList = nicknameService.findBeforeAdd();
        PageInfo<Runner> runnerPageInfo = runnerService.findRunner(0, name, phone);
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("runnerList", runnerPageInfo.getList());
        responseData.put("nicknameList", nicknameList);
        return responseData;
    }
}
